package com.study.Link;

/**
 * - *@ClassName UnidirectionalRingLinkList
 * - *@Author LiuHuaBing
 * - *@Date 2021/11/29 14:25
 * - *@Version 1.0
 * - 单向环形链表
 */
public class UnidirectionalRingLinkList {

    private Node first;

    public UnidirectionalRingLinkList() {
        this.first = new Node();
    }

    public Node getFirst() {
        return first;
    }

    public void setFirst() {
        this.first = new Node();
    }

    public void addElement(Object element) {
        Node node = new Node();
        node.element = element;
        if (first.next == null) {
            first = node;
            first.next = first;
            return;
        }
        Node current = first;
        while (true){
            // 找到环形单向链表的最后一个元素
            if (current.next == first){
                current.next = node;
                node.next =first;
                break;
            }
            current =current.next;
        }
    }
    // 遍历单向环形链表
    public void list(){
        if (this.first.next == null){
            System.out.println("this UnidirectionalRingLinkList is null");
            return;
        }
        Node node = this.first;

        while (true){
            if (node.next != first){
                System.out.println(node.element);
                node =node.next;
            }else {
                System.out.println("the last one is " + node.element);
                break;
            }
        }
    }

}
